package cn.edu.gzist.chapter08.service;


import cn.edu.gzist.chapter08.entity.User;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MessageProducerService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
/*    private AmqpAdmin amqpAdmin;
    private RabbitTemplate rabbitTemplate;
    @Autowired
    public  MessageProducerService(AmqpAdmin amqpAdmin,RabbitTemplate rabbitTemplate) {
        this.amqpAdmin=amqpAdmin;
        this.rabbitTemplate=rabbitTemplate;
        // 1、定义fanout类型的交换机
        this.amqpAdmin.declareExchange(new FanoutExchange("pub/sub.exchange"));
        // 2、定义两个默认持久化队列，分别处理email和sms
        this.amqpAdmin.declareQueue(new Queue("email.queue"));
        this.amqpAdmin.declareQueue(new Queue("sms.queue"));
        // 3、将队列分别与交换机进行绑定
        this.amqpAdmin.declareBinding(new Binding("email.queue",
                Binding.DestinationType.QUEUE, "pub/sub.exchange", "", null));
        this.amqpAdmin.declareBinding(new Binding("sms.queue",
                Binding.DestinationType.QUEUE, "pub/sub.exchange", "", null));
    }*/
    public void psubPublisher(User user) {
        this.rabbitTemplate.convertAndSend("pub/sub.exchange","",user);
    }
    public void routingPublisher(String level,String msg) {
        this.rabbitTemplate.convertAndSend("routing.exchange","routingkey."+level,msg);
    }
    public void topicPublisher(String routingkey,String msg) {
        this.rabbitTemplate.convertAndSend("topic.exchange",routingkey,msg);
    }
}
